package com.walter.learn.Util;

import org.junit.Test;

/**
 * @ProjectName: learn
 * @Package: com.walter.learn.Util
 * @ClassName: ConvertPhotoToWebpByCmd
 * @Author: DELL
 * @Description: 使用谷歌的转换器cmd命令去转换图片格式为webp
 * @Date: 2019/8/15 17:51
 * @Version: 1.0
 */
public class ConvertPhotoToWebpByCmd {
    private static String cmdPath="\\src\\main\\resources\\convertCmd\\bin\\";
    private static String proJectpath=System.getProperty("user.dir");


    public static boolean executeCWebp(String inputFile, String outputFile, Integer quality) {
        boolean result = false;
        String cwebpPath = proJectpath+cmdPath+"cwebp.exe";
        if (inputFile.endsWith(".gif")){
            cwebpPath = proJectpath+cmdPath+"gif2webp.exe";
        }
        try {
            Runtime.getRuntime().exec(cwebpPath).waitFor();
            StringBuilder command = new StringBuilder(cwebpPath);
            command.append(" -q " + (quality == 0 ? 75 : quality));
            command.append(" " + inputFile);
            command.append(" -o " + outputFile);
            Runtime.getRuntime().exec(command.toString());
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
